package com.xxx.car.controller;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.xxx.car.light.LightDLLInterface;
import com.xxx.car.service.SmsSendService;

@RestController
@RequestMapping(value="/api/sms")
public class SmsController {
	
	private final static Map<String,Map<Integer, String>> phones = new HashMap<String,Map<Integer,String>>();
	static{
		HashMap<Integer,String> map = new HashMap<Integer,String>();
		map.put(1, "13917738932");
		map.put(2, "15221559674");
		phones.put("1", map);
	}
	
	@Autowired
	private SmsSendService smsSendService;

	@RequestMapping(value="/phoneMap",method={RequestMethod.GET})
	public Map<String,Map<Integer, String>> sendSms2(final HttpServletRequest req,String mobile,String text){
		return phones;
	}
	@RequestMapping(value="/updatePhone/{repositryId}/{day}/{phone}",method={RequestMethod.GET})
	public Integer updatePhone(@PathVariable("repositryId")String repositryId,@PathVariable("day")Integer day,@PathVariable("phone")String phone){
		if(phones.containsKey(repositryId)){
			phones.get(repositryId).put(day, phone);
		}else {
			HashMap<Integer,String> map = new HashMap<Integer,String>();
			map.put(day, phone);
			phones.put(repositryId, map);
		}
		return 0;
	}
	@RequestMapping(value="/deletePhone/{repositryId}/{day}",method={RequestMethod.GET})
	public Integer deletePhone(@PathVariable("repositryId")String repositryId,@PathVariable("day")Integer day){
		if(phones.containsKey(repositryId)){
			phones.get(repositryId).remove(day);
		}
		return 0;
	}
	
	
	/**
	 * var smsParams = {
                fitName:$scope.latestHit.result2.fit_name,
                name:$scope.latestHit.result2.name,
                cameraName:$scope.latestHit.result1.fit_name,
                score:$scope.latestHit.hit_detail.hit_similarity,
                fitTime:$scope.latestHit.result1.timestamp
              };
	 * @param json
	 * @return
	 */
	@RequestMapping(value="/send",method=RequestMethod.POST)
	public Map<String, String> sendSms(@RequestBody JSONObject json){
		Map<String, String> result = new HashMap<String, String>(); 
		System.out.println("发短信请求参数:"+json.toString());
		String message = "【锦福信息】{0}中{1}于{2}被{3}摄像头拍摄到，相似度为{4}。"; 
		String fitName = json.getString("fitName") + "头像库";
		String name = "嫌疑犯:"+json.getString("name");
		String cameraName = json.getString("cameraName");
		String score = json.getString("score");
		String fitTime = json.getString("fitTime");
		String repositryId = json.getString("repositryId");
		try {
			fitTime = fitTime +"000";
			Date date = new Date(Long.parseLong(fitTime));
			SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
			fitTime = myFmt.format(date);
		} catch (Exception e) {
		}
		Object[] array = new Object[]{fitName,name,fitTime,cameraName,score}; 
		String value = MessageFormat.format(message, array);
		if(!StringUtils.isEmpty(repositryId)){
			if(phones.containsKey(repositryId)){
				Set<Entry<Integer, String>> entrys = phones.get(repositryId).entrySet();
				for(Entry<Integer, String> entry:entrys){
					result.put(entry.getValue(), smsSendService.sendSms(value, entry.getValue()));
				}
			}else {
				System.out.println("仓库"+repositryId + ":" + fitName + " --没有配置报警手机号码.");
			}
		}else {
			System.out.println("仓库id没有传过来");
		}
		return result;
	}
	
	@RequestMapping(value="/light",method=RequestMethod.GET)
	public Map<String, String> light(){
		Map<String, String> result = new HashMap<String, String>();
		try {
			int PowerOnEx1 =  LightDLLInterface.instanceDll.PowerOnEx1();
			System.out.println("开启报警灯:" + PowerOnEx1 );
		    Thread.sleep(2000);
			int PowerOffEx1 = LightDLLInterface.instanceDll.PowerOffEx1();
			System.out.println("关闭报警灯:"+PowerOffEx1);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			LightDLLInterface.instanceDll.PowerOffEx1();
		}
		return result;
	}
	
	
}
